package cn.edu.sasu.controller;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;
import cn.edu.sasu.domain.dto.ChangDocDTO;
import cn.edu.sasu.domain.dto.PatientSearchDTO;
import cn.edu.sasu.domain.entity.Patient;
import cn.edu.sasu.domain.vo.PatientVO;
import cn.edu.sasu.service.PatientService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能：
 * 作者：zwf
 * 日期：2024/4/27 14:44
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;


    /**
     *     查询患者信息信息
     */
    @GetMapping("/findPatientAll")
    public LayuiRespResult findPatientAll(Integer page, Integer limit){
        return patientService.findPatientAll(page,limit);
    }

    @GetMapping("/findPatientAllPatient")
    public LayuiRespResult findPatientAllPatient(Integer page, Integer limit, Integer doctorId){
        return patientService.activityListPatient(page,limit,doctorId);
    }
    /**
     * 保存
     * @param patient
     * @return
     */
    @PostMapping("/save")
    public RespResult save(@RequestBody Patient patient){
        return patientService.save(patient);
    }


    // 批量删除
    @PostMapping("/batchdel")
    public RespResult batchdel(Integer[] userIds){
        return patientService.batchdel(userIds);
    }

    //查询医生信息
    @GetMapping("/getDoctor")
    public RespResult getAllDoctor(){
        return patientService.getDoctor();
    }

    //更新患者信息
    @PostMapping("/update")
    public RespResult update(@RequestBody PatientVO patient){
        if(patient.getDoctorName()==null||patient.getDoctorName().length()<=0){
            return RespResult.error("选择医生不能为空");
        }
        return patientService.update(patient);
    }

    //更新患者信息
    @PostMapping("/changD")
    public RespResult changD(@RequestBody ChangDocDTO changDocDTO){
        return patientService.changD(changDocDTO);
    }

    // 搜索
    @GetMapping("/search")
    public LayuiRespResult search(PatientSearchDTO patientSearchDTO){
        return patientService.search(patientSearchDTO);
    }

    //查询每个地方的患者数量
    @GetMapping("/getCount")
    public RespResult getCount(){
        return patientService.getCount();
    }
    @GetMapping("/chartsPatient")
    public RespResult chartsPatient() {
        List<Patient> list = patientService.findAll();
        Map<Object, Long> collect = list.stream().collect(Collectors.groupingBy(p -> DateUtil.format(p.getCreateTime(), "yyyy-MM-dd"),Collectors.counting()));
        //最后返回给前端的数据
        List<Map<String,Object>> mapList=new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)){
            for (Object key:collect.keySet()){
                Map<String,Object> map=new HashMap<>();
                map.put("name",key);
                map.put("value",collect.get(key));
                mapList.add(map);
            }
        }
        return RespResult.success(mapList);
    }
}
